/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nhadat.dao;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import nhadat.entity.TblLoaiNha;
import nhadat.entity.TblTinDang;
import nhadat.hibernate.HibernateUtil;
import nhadat.tindangfull.TindangfullType;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;

/**
 *
 * @author DUNGND60190
 */
public class TindangDao {

    private Session session;

    public TindangDao() {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

//    public static void main(String[] args) {
//        TindangDao test = new TindangDao();
//        TblTinDang tindang = new TblTinDang();
//        TblLoaiNha loainha = new TblLoaiNha();
//        //loainha.setMaloainha("LN0101");
//        loainha.setTenloainha("Nhà cấp 4");
//        tindang.setTblLoaiNha(loainha);
//        tindang.setTieudetin("adssadasd");
//        test.insertTindang(tindang);
//    }
    public TblTinDang[] getAllTindang() {
        Transaction trans = null;
        try {

            trans = session.getTransaction();
            trans.begin();
            String sql = "From TblTinDang";
            Query query = session.createQuery(sql);

            List list = query.list();
            TblTinDang[] result = new TblTinDang[list.size()];

            session.flush();
            trans.commit();
            if (!list.isEmpty()) {
                list.toArray(result);
                return result;
            }
        } catch (Exception e) {
            if (trans.isActive()) {
                trans.rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public TblTinDang[] getAllTindangByID(String matindang) {
        Transaction trans = null;
        try {
            trans = session.getTransaction();
            trans.begin();
            String sql = "From TblTinDang where matindang = ?";
            Query query = session.createQuery(sql);
            query.setString(0, matindang);

            List list = query.list();
            TblTinDang[] result = new TblTinDang[list.size()];

            session.flush();
            trans.commit();
            if (!list.isEmpty()) {
                list.toArray(result);
                return result;
            }
        } catch (Exception e) {
            if (trans.isActive()) {
                trans.rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public void insertTindang(TblTinDang tindang) {
        Transaction trans = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            trans = session.getTransaction();
            trans.begin();
            session.save(tindang);
            session.flush();
            trans.commit();

        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
    }

    public void updateTindang(TblTinDang newTindang, int matindang) {
        Transaction trans = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            trans = session.getTransaction();
            trans.begin();
            TblTinDang tindang = (TblTinDang) session.get(TblTinDang.class, matindang);
            tindang.setMatindang(newTindang.getMatindang());
            tindang.setTblLoaiNha(newTindang.getTblLoaiNha());
            tindang.setTblLoaiGiayTo(newTindang.getTblLoaiGiayTo());
            tindang.setThongtinchitiet(newTindang.getThongtinchitiet());
            tindang.setCautruc(newTindang.getCautruc());
            //dientichkhuongvien
            tindang.setDvddientichkhuonvien(newTindang.getDvddientichkhuonvien());
            tindang.setDientichkhuonvien(newTindang.getDientichkhuonvien());
            tindang.setChieungangkhuonvien(newTindang.getChieungangkhuonvien());
            tindang.setDvdchieungangkhuonvien(newTindang.getDvdchieungangkhuonvien());
            tindang.setChieudaikhuonvien(newTindang.getChieudaikhuonvien());
            tindang.setDvdchieudaikhuonvien(newTindang.getDvdchieudaikhuonvien());
            //dientichxaydung
            tindang.setDvddientichxaydung(newTindang.getDvddientichxaydung());
            tindang.setDientichxaydung(newTindang.getDientichxaydung());
            tindang.setChieudaixaydung(newTindang.getChieudaixaydung());
            tindang.setDvdchieudaixaydung(newTindang.getDvdchieudaixaydung());
            tindang.setChieungangxaydung(newTindang.getChieungangxaydung());
            tindang.setDvdchieungangxaydung(newTindang.getDvdchieungangxaydung());
            //khuvuc-diachi
            tindang.setDiachi(newTindang.getDiachi());
            tindang.setTblKhuVuc(newTindang.getTblKhuVuc());
            tindang.setGiatien(newTindang.getGiatien());
            tindang.setDvdgiatien(newTindang.getDvdgiatien());
            tindang.setHinhanh(newTindang.getHinhanh());
            tindang.setTieudetin(newTindang.getTieudetin());
            tindang.setTblLoaiTin(newTindang.getTblLoaiTin());
            tindang.setSoluotxem(newTindang.getSoluotxem());
            Date date = new Date();
            tindang.setLancuoicapnhat(date);
            //tindang.setIsActived(isActive);

            session.save(tindang);
            session.flush();
            trans.commit();

        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();

        }
    }

    public void updateTindang(TblTinDang newTindang, int matindang, boolean isActive) {
        Transaction trans = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            trans = session.getTransaction();
            trans.begin();
            TblTinDang tindang = (TblTinDang) session.get(TblTinDang.class, matindang);
            tindang.setMatindang(newTindang.getMatindang());
            tindang.setTblLoaiNha(newTindang.getTblLoaiNha());
            tindang.setTblLoaiGiayTo(newTindang.getTblLoaiGiayTo());
            tindang.setThongtinchitiet(newTindang.getThongtinchitiet());
            tindang.setCautruc(newTindang.getCautruc());
            //dientichkhuongvien
            tindang.setDvddientichkhuonvien(newTindang.getDvddientichkhuonvien());
            tindang.setDientichkhuonvien(newTindang.getDientichkhuonvien());
            tindang.setChieungangkhuonvien(newTindang.getChieungangkhuonvien());
            tindang.setDvdchieungangkhuonvien(newTindang.getDvdchieungangkhuonvien());
            tindang.setChieudaikhuonvien(newTindang.getChieudaikhuonvien());
            tindang.setDvdchieudaikhuonvien(newTindang.getDvdchieudaikhuonvien());
            //dientichxaydung
            tindang.setDvddientichxaydung(newTindang.getDvddientichxaydung());
            tindang.setDientichxaydung(newTindang.getDientichxaydung());
            tindang.setChieudaixaydung(newTindang.getChieudaixaydung());
            tindang.setDvdchieudaixaydung(newTindang.getDvdchieudaixaydung());
            tindang.setChieungangxaydung(newTindang.getChieungangxaydung());
            tindang.setDvdchieungangxaydung(newTindang.getDvdchieungangxaydung());
            //khuvuc-diachi
            tindang.setDiachi(newTindang.getDiachi());
            tindang.setTblKhuVuc(newTindang.getTblKhuVuc());
            tindang.setGiatien(newTindang.getGiatien());
            tindang.setDvdgiatien(newTindang.getDvdgiatien());
            tindang.setHinhanh(newTindang.getHinhanh());
            tindang.setTieudetin(newTindang.getTieudetin());
            tindang.setTblLoaiTin(newTindang.getTblLoaiTin());
            tindang.setSoluotxem(newTindang.getSoluotxem());
            Date date = new Date();
            tindang.setLancuoicapnhat(date);
            tindang.setIsActived(isActive);

            session.save(tindang);
            session.flush();
            trans.commit();

        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();

        }
    }

    public void deleteTindang(int matindang) {
        Transaction trans = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            trans = session.getTransaction();
            trans.begin();
            TblTinDang tindang = (TblTinDang) session.get(TblTinDang.class, matindang);
            session.delete(tindang);
            session.flush();
            trans.commit();

        } catch (Exception e) {
            if (trans.isActive()) {
                trans.rollback();
            }
            e.printStackTrace();
        }


    }

    public String[] getAllDvdGiaTien() {
        Transaction trans = null;
        try {
            trans = session.getTransaction();
            trans.begin();

            Criteria criteria = (Criteria) session.createCriteria(TblTinDang.class);
            criteria.setProjection(Projections.distinct(Projections.property("dvdgiatien")));
            List list = criteria.list();

            session.flush();
            trans.commit();

            if (!list.isEmpty()) {
                String[] allDvdGiaTien = new String[list.size()];

                list.toArray(allDvdGiaTien);

                return allDvdGiaTien;
            }
        } catch (Exception e) {
            if (trans.isActive()) {
                trans.rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public int getMaTinDangLonNhat() {
        int maxMaTinDang = -1;
        Transaction trans = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            trans = session.getTransaction();
            trans.begin();
            String sql = "From TblTinDang Order by matindang desc";
            Query query = session.createQuery(sql).setMaxResults(1);

            List list = query.list();
            if (list.size() > 0) {
                TblTinDang result = (TblTinDang) list.get(0);
                maxMaTinDang = result.getMatindang();
            }

            session.flush();
            trans.commit();
        } catch (Exception e) {
            if (trans.isActive()) {
                trans.rollback();
            }
            e.printStackTrace();
        }
        return maxMaTinDang;
    }

    public TblTinDang[] getAllTindangByDate() {
        Transaction trans = null;
        try {

            trans = session.getTransaction();
            trans.begin();
            String sql = "From TblTinDang";
            Query query = session.createQuery(sql);

            List list = query.list();

            session.flush();
            trans.commit();
            if (!list.isEmpty()) {
                ArrayList TindangByDate = new ArrayList();
                Date date = new Date();
                String currentDate = String.valueOf(date.getDate()) + String.valueOf(date.getMonth())
                        + String.valueOf(date.getYear());
                TblTinDang[] result = new TblTinDang[list.size()];
                for (int i = 0; i < result.length; i++) {
                    TblTinDang tblTinDang = result[i];
                    Date dateFromTindang = tblTinDang.getNgaydangtin();
                    String dateTindang = String.valueOf(dateFromTindang.getDate()) + String.valueOf(dateFromTindang.getMonth())
                            + String.valueOf(dateFromTindang.getYear());

                    if (currentDate.equals(dateTindang)) {
                        TindangByDate.add(tblTinDang);
                    }
                }
                TindangByDate.toArray(result);
                return result;
            }
        } catch (Exception e) {
            if (trans.isActive()) {
                trans.rollback();
            }
            e.printStackTrace();
        }
        return null;
    }
}
